from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
from faker import Faker
from elements import *
faker = Faker(locale='zh_cn')
name = faker.name()
class CRMEB:
    def __init__(self,url,driver):
        self.url = url
        self.driver =driver
    def open_browser(self):
        self.driver.get(self.url)
        self.driver.maximize_window()
    def login(self,username,password):
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(userPassword))[0].send_keys(username)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(userPassword))[
            1].send_keys(password)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(loginBtn)).click()
        # 等待页面主页加载
        sleep(5)
    def addUserGroup(self):
        # 点击用户tap
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(userIcon))[1].click()
        # 点击用户分组
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(userGroup)).click()
        sleep(2)
        # 点击添加分组
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(addGroupIcon)).click()
        sleep(1)
        # 输入分组名称
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(inputGroupUser))[2].send_keys(str(name))
        # 点击确定按钮
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(acceptIcon))[-1].click()
        sleep(3)  # 等待刚新增的分组加上去了
        # 加断言，确认刚新增的分组加上去了
        text = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, f'//*[text()="{name}"]'))).text
        assert text == name
    def updateUserGroup(self,group:str):
        # 修改刚新增的分组
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(updateGroup))[-1].click()
        sleep(2)
        # 输入修改分组名称
        group_name = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(inputUpdateGroup))[2]
        group_name.clear()
        group_name.send_keys(group)
        # 点击确定按钮
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(acceptIcon2))[-1].click()
        sleep(3)
    def deleteGroup(self):
        # 删除刚才修改的分组
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(deleteGroup))[-1].click()
        sleep(2)
        # 点击确认删除按钮
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(acceptDeleteIcon))[-1].click()
        # 等待页面
        sleep(10)
if __name__ == '__main__':
    # 实例化对象
    driver = webdriver.Chrome()
    c1 = CRMEB(driver=driver,url='http://47.99.66.20:8899/admin/login')
    # 打开浏览器
    c1.open_browser()
    # 调用登录
    c1.login('admin','123456')
    # 调用新增用户分组
    c1.addUserGroup()
    # 修改用户分组
    c1.updateUserGroup("测试一下,你就知道了")
    # 删除用户分组
    c1.deleteGroup()


# driver = webdriver.Chrome()

# 打开网页
# driver.get("http://47.99.66.20:8899/admin/login")

# 全屏
# driver.maximize_window()

# 输入账号
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-input__inner')))[0].send_keys("admin")

# 输入密码
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-input__inner')))[1].send_keys("123456")

# 点击登录按钮
# WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.CLASS_NAME,'el-button'))).click()
# 等待页面主页加载
# sleep(5)

# 点击用户tap
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'font12')))[1].click()

# 点击用户分组
# WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[text()="用户分组"]'))).click()
# sleep(2)

#点击添加分组
# WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[text()="添加分组"]'))).click()
# sleep(1)

#输入分组名称
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-input__inner')))[2].send_keys(str(name))

# 点击确定按钮
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-button')))[-1].click()
# sleep(3)        # 等待刚新增的分组加上去了

# 加断言，确认刚新增的分组加上去了
# text = WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,f'//*[text()="{name}"]'))).text
# assert text == name

# 修改刚新增的分组
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.XPATH,'//*[text()="修改"]')))[-1].click()
# sleep(2)

#输入修改分组名称
# group_name = WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-input__inner')))[2]
# group_name.clear()
# group_name.send_keys('测试一下你就知道')

# 点击确定按钮
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-button')))[-1].click()
# sleep(3)        # 等待刚新增的分组加上去了

# 删除刚才修改的分组
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.XPATH,'//*[text()="删除"]')))[-1].click()
# sleep(2)

#点击确认删除按钮
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,'el-button')))[-1].click()

# 等待页面
# sleep(10)
